//
//  RegisterViewController.m
//  inquiry
//
//  Created by 高宇 on 2020/5/19.
//  Copyright © 2020 高宇. All rights reserved.
//

#import "RegisterViewController.h"
#import "ULNetHelper.h"
#import "Interface.h"
#import "NSString+MD5.h"
#import "GXToast.h"
#import "CompleteViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *password2TF;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.numberTF.delegate = self;
    self.passwordTF.delegate = self;
    self.password2TF.delegate = self;
}

- (IBAction)registerClick:(id)sender {
    if ([self.numberTF.text isEqualToString:@""]) {
        [GXToast showText:@"请填写身份证"];
        return;
    }else if ([self.passwordTF.text isEqualToString:@""] || [self.password2TF.text isEqualToString:@""]) {
        [GXToast showText:@"请填写密码"];
        return;
    }else if (![self.passwordTF.text isEqualToString:self.password2TF.text]) {
        [GXToast showText:@"两次密码需一致"];
        return;
    }
    
    self.registerButton.enabled = NO;
    
    NSString *time = [self nowTimeSwitchTimestamp];
    NSString *password = [self.passwordTF.text MD5];
    NSString *token = [[NSString stringWithFormat:@"%@-%@-1000",password,time] MD5];
    NSDictionary *dic = @{
        @"userName":self.numberTF.text,
        @"password":password,
        @"token":token,
        @"time":time,
    };
    [ULNetHelper POST:registerPath parameters:dic success:^(id responseObject) {
        self.registerButton.enabled = YES;
        NSLog(@"responseObject = %@",responseObject);
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            [GXToast showText:@"注册成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                CompleteViewController *vc = [[CompleteViewController alloc] init];
                vc.modalPresentationStyle =  UIModalPresentationFullScreen;
                
                NSDictionary *dataDic = dic[@"data"];
                vc.identityId = dataDic[@"uuid"];
                [self presentViewController:vc animated:YES completion:nil];
            });

        }else{
            NSString *msg = responseObject[@"msg"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message: msg preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            }]];
            [self presentViewController:alert animated:YES completion:nil];

        }
    } failure:^(NSError *error) {
        self.registerButton.enabled = YES;
    }];
}

- (IBAction)loginClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)pwShow:(id)sender {
    UIButton *button = sender;
    if (button.selected) {
        [button setImage:[UIImage imageNamed:@"矩形-1"] forState:0];
        self.passwordTF.secureTextEntry = YES;
    }else{
        [button setImage:[UIImage imageNamed:@"矩形-2"] forState:0];
        self.passwordTF.secureTextEntry = NO;
    }
    button.selected = !button.selected;
}

- (IBAction)psShow2:(id)sender {
    UIButton *button = sender;
    if (button.selected) {
        [button setImage:[UIImage imageNamed:@"矩形-1"] forState:0];
        self.password2TF.secureTextEntry = YES;
    }else{
        [button setImage:[UIImage imageNamed:@"矩形-2"] forState:0];
        self.password2TF.secureTextEntry = NO;
    }
    button.selected = !button.selected;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}


//获取当前时间戳 （以毫秒为单位）
//返回值格式:1555642454396
//这里注意要获取完全部数值后在(long)强转, 如果(long)[datenow timeIntervalSince1970]*1000 这么写是先强转在*1000,就是秒级别的数据了
- (NSString *)nowTimeSwitchTimestamp{
    
    NSDate *datenow = [NSDate date];//现在时间
    
    //这里如果long不够用,就用(long long)
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
    
    return timeSp;
}

@end
